package com.locationhistory.ui;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.locationhistory.R;
import com.locationhistory.activity.SettingActivity;
import com.locationhistory.bean.LocationHistory;

/**
 * 
 * location history
 * 
 * @author thuanlv1
 *
 */
public class LocationHistoryAdapter extends BaseAdapter {
	private LayoutInflater inflater = null;
	private List<LocationHistory> locationHistoryList;
	private Activity activity;

	
	/**
	 * constructor
	 * 
	 * @param context
	 * @param locationHistoryList
	 */
	public LocationHistoryAdapter(Context context, List<LocationHistory> locationHistoryList) {
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		activity = (Activity) context;
		this.locationHistoryList = locationHistoryList;

	}

	@Override
	public int getCount() {
		return locationHistoryList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	/**
	 * This a row of list
	 * 
	 * @author thuanlv1
	 * 
	 */
	public static class ViewHolder {
		public TextView textName;
		public LinearLayout item;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		View vi = convertView;
		final ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.item, null);
			holder = new ViewHolder();
			holder.textName = (TextView) vi.findViewById(R.id.location);
			holder.item = (LinearLayout) vi.findViewById(R.id.item);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		holder.textName.setText(locationHistoryList.get(position).getLocation());
		holder.item.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(activity, SettingActivity.class);

				activity.startActivity(intent);

			}
		});

		return vi;
	}
}
